/**
 * 
 */
package net.homelinux.ajay.net.ondisk.view.client;



import net.homelinux.ajay.net.ondisk.client.AppPanel;
import net.homelinux.ajay.net.ondisk.model.client.Model;
import net.homelinux.ajay.net.ondisk.view.menu.client.CollectionMenuBuilder;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TreeListener;

/**
 * @author ajay
 *
 */
public class AppTree extends com.google.gwt.user.client.ui.Tree {

  private static final RuntimeException NOIMPLEMENT = new RuntimeException("Not Implemented!!");
  private static AppTree instance;


  public static AppTree makeTree(Panel treepanel,AppPanel app) {
    AppTree tree = new AppTree(app);
    CollectionsTreeItem itm;
    itm = CollectionsTreeItem.makeChild("Collections",tree);
    itm.makeCollection("MIT OCW Lectures");
    itm.makeCollection("Untitled1");
    tree.setSelectedItem(itm,true);
    treepanel.add(tree);
    tree.addTreeListener(new TreeListener(){

      private AppTreeItem oldselection;

      public void onTreeItemSelected(com.google.gwt.user.client.ui.TreeItem item) {
        
        Model.log(item.getText());
       
        if(oldselection == item ){//Should never come here. But just in case.
          
          return;
        }
        if(item == CollectionCommandsTreeItem.getDragTarget()){//Should never come here. But just in case.
          return;
        }
        
        if(oldselection !=null  ){
          oldselection.onDeselect();
        }
        
        oldselection = (AppTreeItem)item;
        oldselection.onSelect();
        
      }

      public void onTreeItemStateChanged(com.google.gwt.user.client.ui.TreeItem item) {
        // noop for now
        
      }
      }    
    );
    

    tree.sinkEvents(Event.MOUSEEVENTS);
    AppTree.instance=tree;
    return tree;
  }



  private AppPanel appPanel;



  /**
   * @param app 
   * 
   */
  public AppTree(AppPanel app) {
    sinkEvents(Event.ONMOUSEUP);
    appPanel = app;
  }

  public void onBrowserEvent(Event event)
  {
    super.onBrowserEvent(event);
    if (DOM.eventGetType(event) == Event.ONMOUSEUP)
    {
      if(DOM.eventGetCtrlKey(event)||DOM.eventGetButton(event)==Event.BUTTON_RIGHT){
        AppTreeItem itemSelected = (AppTreeItem) getSelectedItem();
        itemSelected.onContextClick(event);
        DOM.eventCancelBubble(event, true);
      }
    }
  } 
//  /* (non-Javadoc)
//   * @see com.google.gwt.user.client.ui.Tree#add(com.google.gwt.user.client.ui.Widget)
//   */
//  public void add(Widget widget) {
//    throw NOIMPLEMENT;
//    //super.add(widget);
//  }

  /* (non-Javadoc)
   * @see com.google.gwt.user.client.ui.Tree#addItem(java.lang.String)
   */
  public com.google.gwt.user.client.ui.TreeItem addItem(String itemText) {
    AppTreeItem ret = CollectionsTreeItem.makeChild(itemText,this);
    addItem(ret);
    return ret;
  }

  /* (non-Javadoc)
   * @see com.google.gwt.user.client.ui.Tree#addItem(com.google.gwt.user.client.ui.TreeItem)
   */
  public void addItem(com.google.gwt.user.client.ui.TreeItem item) {
    if(item instanceof AppTreeItem){
      addItem((AppTreeItem)item);
    }
    else{
      throw NOIMPLEMENT;
    }
    //
  }

  public void addItem(AppTreeItem item) {
    super.addItem(item);
  }

  /* (non-Javadoc)
   * @see com.google.gwt.user.client.ui.Tree#getImageBase()
   */
  public String getImageBase() {
    return super.getImageBase();
  }

  /* (non-Javadoc)
   * @see com.google.gwt.user.client.ui.Tree#getItem(int)
   */
  public com.google.gwt.user.client.ui.TreeItem getItem(int index) {
    return (AppTreeItem) super.getItem(index);
  }



  public static AppTree getTree() {
    return instance;
  }



  public AppPanel getAppPanel() {
    return appPanel;
  }


//  private void onDragToTree(Element source2, Element destination) {
//    String href = DOM.getAttribute(source2, "href");
//    if(href!=null){
//      urlbox.setText(href);
//      addUrl2Collection();
//    }
//  }
  
//  
//  public void onBrowserEvent(Event e){
//    Element source;
//    switch(DOM.eventGetType(e)){
////      case Event.ONMOUSEDOWN: sourcetmp = DOM.eventGetTarget(e);break;
////      case Event.ONMOUSEOUT: source = sourcetmp; break;
//      case Event.ONMOUSEUP:
//        
//        Element destination = DOM.eventGetTarget(e);
//        source = DOM.getCaptureElement();
//        if(destination == source){
//          source = null;
////          sourcetmp = null;
//        }
//        if (source!=null){
//          onDrag(source,destination);
//        }
//        else{
//        }
//        break;
//    }
//    super.onBrowserEvent(e);
//  }
//
//  private void onDrag(Element source2, Element destination) {
//    if(DOM.isOrHasChild(getElement(), destination)
//        //&&
//        //DOM.isOrHasChild(browser.getElement(), source2)
//        )
//      onDragToTree(source2,destination);
//    
//  }



}
